package com.wang.android.library.tool;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.blankj.utilcode.util.LogUtils;
import com.wang.android.library.FBaseApplication;

/**
 * Created by Liujc on 2016/4/25.
 * Email liujiangchuan@hotmail.com
 */
public class WSharedPreference
{
    private static final String PATH = "FSharedPreference";
    private SharedPreferences mPreferences;
    private Editor mEditor;

    private static final String TP_TOKEN = "tp_auth_token";
    private static final String TP_CODE = "tp_auth_code";
    private static class SingletonClassInstance
    {

    }
    private static WSharedPreference self;

    public static WSharedPreference getInstance()
    {
        if(self == null)
            self = new WSharedPreference();
        return self;
    }

    private WSharedPreference()
    {
        mPreferences =
                FBaseApplication.getInstance().getSharedPreferences(PATH, Activity.MODE_PRIVATE);
        mEditor = mPreferences.edit();
    }

    public void putString(String key, String value)
    {
        if (null != mEditor)
        {
            mEditor.putString(key, value);
            mEditor.apply();
        }
        else
        {
            LogUtils.e("putString mEditor == null");
        }
    }

    public String getString(String key, String defValue)
    {
        String ret = null;
        if (null != mPreferences)
        {
            ret = mPreferences.getString(key, defValue);
        }
        else
        {
            LogUtils.e("getString mPreferences == null");
        }
        return ret;
    }

    public void putBoolean(String key, boolean value)
    {
        if (null != mEditor)
        {
            mEditor.putBoolean(key, value);
            mEditor.apply();
        }
        else
        {
            LogUtils.e("putBoolean mEditor == null");
        }
    }

    public boolean getBoolean(String key, boolean defValue)
    {
        boolean ret = false;
        if (null != mPreferences)
        {
            ret = mPreferences.getBoolean(key, defValue);
        }
        else
        {
            LogUtils.e("getBoolean mPreferences == null");
        }
        return ret;
    }

    public void putFloat(String key, float value)
    {
        if (null != mEditor)
        {
            mEditor.putFloat(key, value);
            mEditor.apply();
        }
        else
        {
            LogUtils.e("putFloat mEditor == null");
        }
    }

    public float getFloat(String key, float defValue)
    {
        float ret = 0;
        if (null != mPreferences)
        {
            ret = mPreferences.getFloat(key, defValue);
        }
        else
        {
            LogUtils.e("getFloat mPreferences == null");
        }
        return ret;
    }

    public void putInt(String key, int value)
    {
        if (null != mEditor)
        {
            mEditor.putInt(key, value);
            mEditor.apply();
        }
        else
        {
            LogUtils.e("putInt mEditor == null");
        }
    }

    public int getInt(String key, int defValue)
    {
        int ret = 0;
        if (null != mPreferences)
        {
            ret = mPreferences.getInt(key, defValue);
        }
        else
        {
            LogUtils.e("getInt mPreferences == null");
        }
        return ret;
    }

    public void putLong(String key, long value)
    {
        if (null != mEditor)
        {
            mEditor.putLong(key, value);
            mEditor.apply();
        }
        else
        {
            LogUtils.e("putLong mEditor == null");
        }
    }

    public long getLong(String key, long defValue)
    {
        long ret = 0;
        if (null != mPreferences)
        {
            ret = mPreferences.getLong(key, defValue);
        }
        else
        {
            LogUtils.e("getLong mPreferences == null");
        }
        return ret;
    }

    public void remove(String key)
    {
        if (null != mEditor)
        {
            mEditor.remove(key);
            mEditor.apply();
        }
    }

    public void clear()
    {
        if (null != mEditor)
        {
            mEditor.clear();
            mEditor.apply();
        }
    }
    public void setTpToken(String token)
    {
        if (mEditor == null)
        {
            return;
        }
        mEditor.putString(TP_TOKEN, token);
        mEditor.commit();
    }

    public String getTpToken()
    {
        if (mPreferences == null)
        {
            return "";
        }
        return mPreferences.getString(TP_TOKEN, "");
    }

    public void setTpCode(String token)
    {
        if (mEditor == null)
        {
            return;
        }
        mEditor.putString(TP_CODE, token);
        mEditor.commit();
    }

    public String getTpCode()
    {
        if (mPreferences == null)
        {
            return "";
        }
        return mPreferences.getString(TP_CODE, "");
    }
}
